# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Renato Silva <br.renatosilva@gmail.com>

pkgbase="sqlite"
pkgname=('sqlite' 'libsqlite' 'libsqlite-devel' 'sqlite-doc'
        'tcl-sqlite' 'sqlite-compress' 'sqlite-vfslog' 'sqlite-rbu'
        'sqlite-icu'  'sqlite-extensions')
_amalgamationver=3300000
_docver=${_amalgamationver}
pkgver=3.30.0
pkgrel=1
pkgdesc="A C library that implements an SQL database engine"
arch=('i686' 'x86_64')
license=(PublicDomain)
url="https://www.sqlite.org/"
makedepends=('libreadline-devel' 'icu-devel' 'zlib-devel' 'tcl')
source=( # tarball containing the amalgamation for SQLite >= 3.7.5 together with a configure script and makefile for building it; includes now also the Tcl Extension Architecture (TEA)
        https://www.sqlite.org/2019/sqlite-autoconf-${_amalgamationver}.tar.gz
        https://www.sqlite.org/2019/sqlite-doc-${_docver}.zip
        LICENSE
        'sqlite3-3.30.0-1.src.patch'
        'sqlite-3.28.0-msys2.patch')
options=('!libtool' '!emptydirs' '!debug' 'strip')
sha256sums=('e0a8cf4c7a87455e55e10413d16f358ca121ccec687fe1301eac95e2d340fc58'
            'cb4deed80e94c7c0d7ee4a5c76d9d91e4deec48e8b158b473b27253fbfc891d6'
            '0b76663a90e034f3d7f2af5bfada4cedec5ebc275361899eccc5c18e6f01ff1f'
            '95179493fda34cf56d60adbee51f97dfd30f50cadb038e537adbfc30b11a91cf'
            'fd301fe04600d5a91045dfc4dd7d3e95f0c18b6df82239fcd3bac1a176141b2a')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

apply_patch_with_msg_p2() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp2 -i "${srcdir}/${_patch}"
  done
}


del_file_exists() {
  for _fname in "$@"
  do
    if [ -f $_fname ]; then
      rm -rf $_fname
    fi
  done
}
# =========================================== #

_PKG_EXTENSIONS_DLL=(usr/bin/msys-sqlite3amatch-0.dll
                     usr/bin/msys-sqlite3anycollseq-0.dll
                     usr/bin/msys-sqlite3carray-0.dll
                     usr/bin/msys-sqlite3closure-0.dll
                     usr/bin/msys-sqlite3completion-0.dll
                     usr/bin/msys-sqlite3csv-0.dll
                     usr/bin/msys-sqlite3eval-0.dll
                     usr/bin/msys-sqlite3fileio-0.dll
                     usr/bin/msys-sqlite3fuzzer-0.dll
                     usr/bin/msys-sqlite3ieee754-0.dll
                     usr/bin/msys-sqlite3memvfs-0.dll
                     usr/bin/msys-sqlite3nextchar-0.dll
                     usr/bin/msys-sqlite3percentile-0.dll
                     usr/bin/msys-sqlite3regexp-0.dll
                     usr/bin/msys-sqlite3remember-0.dll
                     usr/bin/msys-sqlite3vtshim-0.dll
                     usr/bin/msys-sqlite3completion-0.dll
                     usr/bin/msys-sqlite3rot13-0.dll
                     usr/bin/msys-sqlite3series-0.dll
                     usr/bin/msys-sqlite3stmt-0.dll
                     usr/bin/msys-sqlite3completion-0.dll
                     usr/bin/msys-sqlite3sha1-0.dll
                     usr/bin/msys-sqlite3shathree-0.dll
                     usr/bin/msys-sqlite3showauth-0.dll
                     usr/bin/msys-sqlite3spellfix-0.dll
                     usr/bin/msys-sqlite3totype-0.dll
                     usr/bin/msys-sqlite3unionvtab-0.dll
                     usr/bin/msys-sqlite3vtshim-0.dll
                     usr/bin/msys-sqlite3vfsstat-0.dll
                     usr/bin/msys-sqlite3wholenumber-0.dll)
_PKG_RBU_DLL=(usr/bin/msys-sqlite3rbu-0.dll)
_PKG_RBU_EXE=(usr/bin/rbu.exe)
_PKG_VFSLOG_DLL=(usr/bin/msys-sqlite3vfslog-0.dll)
_PKG_ICU_DLL=(usr/bin/msys-sqlite3icu-0.dll)
_PKG_COMPRESSION_DLL=(usr/bin/msys-sqlite3compress-0.dll)
_PKG_TCL=(usr/lib/sqlite${pkgver}/pkgIndex.tcl
          usr/lib/sqlite${pkgver}/libsqlite${pkgver}.dll
          usr/share/man/mann/sqlite3.n)

prepare() {
  cd "${srcdir}"/sqlite-autoconf-${_amalgamationver}
  del_file_exists , README README.first \
    appendvfs.c  amatch.c anycollseq.c blobio.c btreeinfo.c \
    carray.c closure.c compress.c completion.c csv.c dbdump.c eval.c explain.c \
    fileio.c fossildelta.c fuzzer.c icu.c ieee754.c json1.c \
    memstat.c memtrace.c memvfs.c mmapwarm.c \
    nextchar.c normalize.c percentile.c prefixes.c \
    rbu.c regexp.c remember.c rot13.c \
    scrub.c series.c sha1.c shathree.c showauth.c spaceanal.tcl spellfix.c \
    sqlar.c sqldiff.c sqlite3rbu.c sqlite3rbu.h stmt.c \
    templatevtab.c totype.c unionvtab.c \
    vfslog.c vfsstat.c vtshim.c vtablog.c wholenumber.c \
    zipfile.c zorder.c

  apply_patch_with_msg_p2 sqlite3-3.30.0-1.src.patch
  apply_patch_with_msg sqlite-3.28.0-msys2.patch

  autoreconf -fi
}

build() {
  export CPPFLAGS="$CPPFLAGS \
    -DHAVE_MALLOC_H \
    -DHAVE_MALLOC_USABLE_SIZE \
    -DSQLITE_ENABLE_DBSTAT_VTAB=1 \
    -DSQLITE_ENABLE_STAT4=1 \
    -DSQLITE_ENABLE_UNLOCK_NOTIFY=1 \
    -DSQLITE_ENABLE_UPDATE_DELETE_LIMIT=1 \
    -DSQLITE_OMIT_LOOKASIDE \
    -DSQLITE_OS_UNIX=1 \
    -DSQLITE_OS_WIN=1 \
    -DSQLITE_SECURE_DELETE=1 \
    -DSQLITE_ENABLE_SESSION=1 \
    -DSQLITE_ENABLE_PREUPDATE_HOOK=1 \
    -DSQLITE_SOUNDEX=1 \
    -DSQLITE_TEMP_STORE=1 \
    -DSQLITE_USE_FCNTL_TRACE=1 \
    -DUSE_SYSTEM_SQLITE=1 \
    -DSQLITE_ENABLE_FTS3_PARENTHESIS=1"

  cd "${srcdir}"/sqlite-autoconf-$_amalgamationver
  ./configure \
    --prefix=/usr \
    --sysconfdir=/etc \
    --localstatedir=/var \
    --build=${CHOST} \
    --enable-shared \
    --enable-static \
    --enable-readline --disable-editline \
    --enable-fts5 --enable-json1

  make
  make check 
  make DESTDIR=${srcdir}/dest install
  cd "${srcdir}"/sqlite-autoconf-$_amalgamationver/tea
  CFLAGS+=" -I${srcdir}/sqlite-autoconf-$_amalgamationver/" \
  LIBS+=" -L${srcdir}/sqlite-autoconf-$_amalgamationver/.libs" \
  ./configure \
    --prefix=/usr \
    --sysconfdir=/etc \
    --localstatedir=/var \
    --build=${CHOST} \
    --with-system-sqlite
  make
  make DESTDIR=${srcdir}/dest install
}

_install_license() {
  install -D -m644 ${srcdir}/LICENSE ${pkgdir}/usr/share/licenses/${pkgname}/LICENSE
}

package_sqlite() {
  pkgdesc="A C library that implements an SQL database engine"
  depends=('libreadline' 'libsqlite')
  provides=("sqlite3=${pkgver}")
  replaces=("sqlite3")

  mkdir -p ${pkgdir}/usr/bin
  cp -f ${srcdir}/dest/usr/bin/*.exe ${pkgdir}/usr/bin/
  cp -rf ${srcdir}/dest/usr/share ${pkgdir}/usr/

# These go into sqlite3-rdu package
  for _f in "${_PKG_RBU_EXE[@]}"
  do
    rm -f ${pkgdir}/${_f}
  done

# These go into tcl-sqlite
  for _f in "${_PKG_TCL[@]}"
  do
    rm -f ${pkgdir}/${_f}
  done
 
  _install_license
}

package_libsqlite() {
  pkgdesc="Sqlite3 library"
  groups=('libraries')

  mkdir -p ${pkgdir}/usr/bin
  cp -f ${srcdir}/dest/usr/bin/*.dll ${pkgdir}/usr/bin/

# These go into sqlite3-compression package
  for _f in "${_PKG_COMPRESSION_DLL[@]}"
  do
    rm -rf ${pkgdir}/${_f}
  done

# These go into sqlite3-vfslog package
  for _f in "${_PKG_VFSLOG_DLL[@]}"
  do
    rm -rf ${pkgdir}/${_f}
  done

# These go into sqlite3-rdu package
  for _f in "${_PKG_RBU_DLL[@]}"
  do
    rm -rf ${pkgdir}/${_f}
  done

# These go into sqlite3-icu package
  for _f in "${_PKG_ICU_DLL[@]}"
  do
    rm -rf ${pkgdir}/${_f}
  done

# These go into sqlite3-extensions package
  for _f in "${_PKG_EXTENSIONS_DLL[@]}"
  do
    rm -rf ${pkgdir}/${_f}
  done
# These go into tcl-sqlite
  for _f in "${_PKG_TCL[@]}"
  do
    rm -f ${pkgdir}/${_f}
  done
  _install_license
}

package_libsqlite-devel() {
  pkgdesc="Sqlite3 headers and libraries"
  groups=('development')
  depends=("libsqlite=${pkgver}")
  optdepends=('sqlite-compress: Zlib extension for SQLite'
              'sqlite-extensions: various extensions for SQLite'
              'sqlite-icu: ICU extension for SQLite'
              'sqlite-rbu: RBU extension for SQLite'
              'sqlite-vfslog: vfslog extension for SQLite'
              'tcl-sqlite: Tcl bindings for SQLite')
  options+=('staticlibs')

  mkdir -p ${pkgdir}/usr
  cp -rf ${srcdir}/dest/usr/include ${pkgdir}/usr/
  cp -rf ${srcdir}/dest/usr/lib ${pkgdir}/usr/
# These go into tcl-sqlite
  for _f in "${_PKG_TCL[@]}"
  do
    rm -f ${pkgdir}/${_f}
  done

  _install_license
}

package_sqlite-doc() {
  pkgdesc="Most of the static HTML files that comprise this website, including all of the SQL Syntax and the C/C++ interface specs and other miscellaneous documentation"
  #arch=('any') - not yet supported
  provides=("sqlite3-doc=${pkgver}")
  replaces=("sqlite3-doc")
  depends=("sqlite=${pkgver}")

  #cd ${srcdir}/sqlite-doc-${_amalgamationver}
  cd ${srcdir}/sqlite-doc-${_docver}
  mkdir -p ${pkgdir}/usr/share/doc/${pkgbase}
  cp -R *  ${pkgdir}/usr/share/doc/${pkgbase}/

  # fix permissions and remove obsolete files; https://bugs.archlinux.org/task/24605
  find ${pkgdir} -type f -perm 755 -exec ls -lha {} \;
  find ${pkgdir} -type f -perm 755 -exec chmod 644 {} \;

  find ${pkgdir} -type f -name '*~' -exec ls -lha {} \;
  find ${pkgdir} -type d -name '*~' -exec ls -lha {} \;
  find ${pkgdir} -name '*~' -exec rm -f {} \;

  find ${pkgdir} -type f -name '.~*' -exec ls -lha {} \; # /build/pkg/sqlite-doc/usr/share/doc/sqlite/images/fileformat/.~lock.indexpage.odg#
  find ${pkgdir} -type d -name '.~*' -exec ls -lha {} \;
  find ${pkgdir} -name '.~*' -exec rm -f {} \;

  _install_license
}

package_sqlite-rbu() {
  pkgdesc="RBU extension for SQLite"
  depends=("libsqlite=${pkgver}")
  options+=('staticlibs')
  mkdir -p ${pkgdir}/usr/bin
  cp ${srcdir}/dest/usr/bin/rbu.exe ${pkgdir}/usr/bin/rbu.exe
  for _f in "${_PKG_RBU_DLL[@]}"
  do
    cp ${srcdir}/dest/${_f} ${pkgdir}/${_f}
  done
  for _f in "${_PKG_RBU_EXE[@]}"
  do
    cp ${srcdir}/dest/${_f} ${pkgdir}/${_f}
  done

   _install_license

}

package_sqlite-extensions() {
  pkgdesc="various extensions for SQLite"
  depends=("libsqlite=${pkgver}")
  options+=('staticlibs')
  mkdir -p ${pkgdir}/usr/bin
  for _f in "${_PKG_EXTENSIONS_DLL[@]}"
  do
    cp ${srcdir}/dest/${_f} ${pkgdir}/${_f}
  done
  _install_license
}

package_sqlite-icu() {
  pkgdesc="ICU extension for SQLite"
  depends=("libsqlite=${pkgver}" 'icu')
  options+=('staticlibs')
  mkdir -p ${pkgdir}/usr/bin
  for _f in "${_PKG_ICU_DLL[@]}"
  do
    cp ${srcdir}/dest/${_f} ${pkgdir}/${_f}
  done
   _install_license
}

package_sqlite-vfslog() {
  pkgdesc="vfslog extension for SQLite"
  depends=("libsqlite=${pkgver}")
  options+=('staticlibs')
  mkdir -p ${pkgdir}/usr/bin
  for _f in "${_PKG_VFSLOG_DLL[@]}"
  do
    cp ${srcdir}/dest/${_f} ${pkgdir}/${_f}
  done
   _install_license
}

package_sqlite-compress() {
  pkgdesc="Zlib extension for SQLite"
  depends=("libsqlite=${pkgver}" 'zlib')
  options+=('staticlibs')
  mkdir -p ${pkgdir}/usr/bin
  for _f in "${_PKG_COMPRESSION_DLL[@]}"
  do
    cp ${srcdir}/dest/${_f} ${pkgdir}/${_f}
  done
   _install_license
}

package_tcl-sqlite() {
  pkgdesc="Tcl bindings for SQLite"
  depends=("libsqlite=${pkgver}" 'tcl')
  options+=('staticlibs')
   mkdir -p ${pkgdir}/usr/lib/sqlite${pkgver}
  mkdir -p ${pkgdir}/usr/share/man/mann
  for _f in "${_PKG_TCL[@]}"
  do
    cp ${srcdir}/dest/${_f} ${pkgdir}/${_f}
  done

   _install_license
}

